package de.sendorian.app.forumArchive.repository;


import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Component;

import de.sendorian.app.forumArchive.domain.File;
import de.sendorian.app.forumArchive.domain.Post;


@Component
public class FileRepository extends RepositoryBase<File> {

	public FileRepository() {
		super(File.class);
	}

	public Post findPostByFile(File file) {
		String jpql = "select p from Post p,  IN(p.images) i, IN(p.archives) a where i.id = :fileId or a.id = :fileId";
		try {
			return (Post) getEntityManager().createQuery(jpql).setParameter("fileId", file.getId())
			    .getSingleResult();
		} catch (EmptyResultDataAccessException e) {
			logger.debug("No Topic found for file " + file);
		}
		return null;
	}

}
